package com.atkusen.mini.minidanke.controller;

import java.util.Arrays;
import java.util.Map;

import com.atkusen.mini.common.config.RedisUtil;
import com.atkusen.mini.common.entity.MiniUserEntity;
import com.atkusen.mini.minidanke.rpc.MiniMbService;
import com.atkusen.mini.minidanke.service.MiniUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.atkusen.mini.common.utils.PageUtils;
import com.atkusen.mini.common.utils.R;

import javax.annotation.Resource;


/**
 * @author Ghl
 * @email 807866012@qq.com
 * @date 2021-05-07 21:30:45
 */
@RestController
@RequestMapping("/web/miniuser")
public class MiniUserController {
    @Autowired
    private MiniUserService miniUserService;
    @Autowired
    private MiniMbService miniMbService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 获取列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = miniUserService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     *  @PreAuthorize("hasAnyAuthority('sys:user:gets','sys:user:adds','sys:user:edits')")
     *     权限只要是有一个就默认有该权限
     *     底层代码也是这样设计的，获取到所有的权限跟目前的权限对比
     *     Set<String> roleSet = getAuthoritySet();
     * 		for (String role : roles) {
     * 			String defaultedRole = getRoleWithDefaultPrefix(prefix, role);
     * 			if (roleSet.contains(defaultedRole)) {
     * 				return true;
     *                        }        *
     *           }
     */
    @GetMapping("/info/{userId}")
    @PreAuthorize("hasAnyAuthority('sys:user:gets','sys:user:adds','sys:user:edits')")
    public R info(@PathVariable("userId") Integer userId) {
        MiniUserEntity miniUser = miniUserService.getById(userId);
        return R.ok().put("miniUser", miniUser);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody MiniUserEntity miniUser) {
        miniUserService.save(miniUser);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody MiniUserEntity miniUser) {
        miniUserService.updateById(miniUser);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Integer[] userIds) {
        miniUserService.removeByIds(Arrays.asList(userIds));
        return R.ok();
    }

    /**
     * 远程调用服务
     */
    @GetMapping("/getUserById/{userId}")
    public R getUserById(@PathVariable("userId") Integer userId) {

        return miniMbService.info(userId);
    }

     @GetMapping("/getCache")
    public R getCache(String key){
         String result = (String) redisUtil.getStringValue("goods:001");
         return R.ok().put(key,result);
     }
}
